class Users::UsersController < ApplicationController
  layout 'manage', except: [:forgot]
	layout 'users', only: [:forgot]
  before_action :set_user, only: [:show, :certify, :comfirm_individual, :comfirm_company, :reject ]
  
  before_action :authenticate_admin!, except:[:forgot]
  
  def index
    if params[:state]
      @users = User.where(workflow_state: params[:state]).order(created_at: :desc).page(params[:page]).per(10)
    else
      @users = User.order(created_at: :desc).page params[:page]
    end
  end

  def show
  end

  def certify
    
    respond_to do |format|
      if @user.can_certify?
        @user.certify!
        format.js { render 'js_user_certification_changed', :layout => false }
      else
        format.js { render :layout => false, :status => :unauthorized  }
      end
    end
  end

  def comfirm_individual
    
    respond_to do |format|
      if @user.can_comfirm_individual?
        @user.comfirm_individual!
        format.js { render 'js_user_certification_changed', :layout => false }
      else
        format.js { render :layout => false, :status => :unauthorized  }
      end
    end
  end

  def comfirm_company
    
    respond_to do |format|
      if @user.can_comfirm_company?
        @user.comfirm_company!
        format.js { render 'js_user_certification_changed', :layout => false }
      else
        format.js { render :layout => false, :status => :unauthorized  }
      end
    end
  end

  def reject
    
    respond_to do |format|
      if @user.can_reject?
        @user.reject!
        format.js { render 'js_user_certification_changed', :layout => false }
      else
        format.js { render :layout => false, :status => :unauthorized  }
      end
    end
  end


  def forgot
    config = Config.where(name: 'login_ad').take
    @ad_image = config.file.url if config.file
    
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_user
      @user = User.find(params[:id])
    end
end
